//
//  UIHelper.swift
//  douyinSwift
//
//  Created by 于波 on 2019/11/20.
//  Copyright © 2019 于波. All rights reserved.
//

import UIKit

var layers = [CAShapeLayer]()

@IBDesignable open class ClippedView: UIView {
    
}

extension UIView {
    // @IBInspectable public 设置圆角在xib中可见
    // 圆角
    @IBInspectable public var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
        }
    }
    
    // 边框
    @IBInspectable public var borderWidth: CGFloat {
        get {
            return layer.borderWidth
        }
        set {
            layer.borderWidth = newValue
        }
    }
    
    // 边框颜色
    @IBInspectable public var borderColor: UIColor {
        get {
            return UIColor.init(cgColor: layer.borderColor!)
        }
        set {
            layer.borderColor = newValue.cgColor
        }
    }
    
    // 阴影色
    @IBInspectable public var shadowColor : UIColor {
        get {
            return UIColor(cgColor: layer.shadowColor!)
        }
        set {
            layer.shadowColor = newValue.cgColor
        }
    }
    
    // 阴影的 位移
    @IBInspectable public var shadowOffset : CGSize  {
        get {
            return layer.shadowOffset
        }
        set {
            layer.shadowOffset = newValue
        }
    }
    
    // 阴影的 圆角
    @IBInspectable public var shadowRadius : CGFloat  {
        get {
            return layer.shadowRadius
        }
        set {
            layer.shadowRadius = newValue
        }
    }
    
    // 阴影的 透明度
    @IBInspectable public var shadowOpacity : Float  {
        get {
            return layer.shadowOpacity
        }
        set {
            layer.shadowOpacity = newValue
            layer.masksToBounds = false //裁边为false 为了透明度有效
        }
    }
    
    
    func raiseAnimate(imgName:String, delay:TimeInterval) {
        let path = UIBezierPath()
        
        // 起点:在视图的中间 midX
        let beginPoint = CGPoint(x: bounds.midX, y: bounds.midY)
        
        //控制点的位移：随机数
        let a : CGFloat = 2
        let b : CGFloat = 1.5
        let c : CGFloat = 3
        
        let offsets1 = [a,b,c]
        
        let cxOffset = offsets1.randomElement()! * bounds.maxX
        let cyOffset = offsets1.randomElement()! * bounds.maxY
        
        //位移的终点：随机数
        let e : CGFloat = 1.5
        let d : CGFloat = 1
        let f : CGFloat = 0.8
        
        let offsets2 = [d,e,f]
        
        let g : CGFloat = 2.5
        let h : CGFloat = 3
        let i : CGFloat = 2
        
        let offsets3 = [g,h,i]
        
        let exOffset = offsets2.randomElement()! * bounds.maxX
        let eyOffset = offsets3.randomElement()! * bounds.maxY
        
        //终点
        let endPoint = CGPoint(x: beginPoint.x - exOffset, y: beginPoint.y - eyOffset)
        
        //控制点
        let controlPoint = CGPoint(x: beginPoint.x - cxOffset, y: beginPoint.y - cyOffset)
        
        path.move(to: beginPoint)
        path.addQuadCurve(to: endPoint, controlPoint: controlPoint)
        
        let group = CAAnimationGroup()
        group.duration = 4
        group.beginTime = CACurrentMediaTime() + delay
        group.repeatCount = .infinity
        group.isRemovedOnCompletion = false
        group.fillMode = .forwards
        // linear线性动画
        group.timingFunction = CAMediaTimingFunction(name: .linear)
        
        // position位置
        let pathAnimation = CAKeyframeAnimation(keyPath: "position")
        pathAnimation.path = path.cgPath
        
        // transform形变 rotation旋转
        let rotateAnimation = CAKeyframeAnimation(keyPath: "transform.rotation")
        rotateAnimation.values = [0, .pi/2.0, .pi/1.0]
        
        //opacity不透明
        let alphaAnimation = CAKeyframeAnimation(keyPath: "opacity")
        alphaAnimation.values = [0, 0.3, 1, 0.3, 0]
        
        // transform形变 scale缩放
        let scaleAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
        scaleAnimation.values = [1.0, 1.8, 2.0]
        
        group.animations = [pathAnimation, rotateAnimation, alphaAnimation, scaleAnimation]
        
        let layer = CAShapeLayer()
        
        layer.opacity = 0
        layer.contents = UIImage(named: imgName)?.cgImage
        layer.frame = CGRect(origin: beginPoint, size: CGSize(width: 10, height: 10))
        self.layer.addSublayer(layer)
        
        layer.add(group, forKey: nil)
        
        layers.append(layer)
    }
    
    func resetViewAnimation() {
        for layer in layers {
            layer.removeAllAnimations()
        }
        self.layer.removeAllAnimations()
    }
}
